#include "graphicscity.h"
#include <QPainter>

QRectF GraphicsCity::BOUNDINGRECT = QRectF(0,0,20,20);
QColor GraphicsCity::LINECOLOR = QColor(255,0,0);

GraphicsCity::GraphicsCity(City *city)
{
    pCity = city;
    hover = onPath = startEnd = false;
    next = nullptr;
    setAcceptHoverEvents(true);
    setFlags(QGraphicsItem::ItemIsSelectable);
}

QPainterPath GraphicsCity::shape() const
{
    QPainterPath path;
    path.addEllipse(boundingRect());
    return path;
}

void GraphicsCity::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    if(pCity == nullptr) return;
    painter->setPen(QPen());
    painter->setBrush(QBrush(QColor(200,200,200)));
    if(hover) painter->setBrush(QBrush(QColor(60,120,180)));
    if(onPath) painter->setBrush(QBrush(QColor(255,0,0)));
    if(startEnd) painter->setBrush(QBrush(QColor(0,255,255)));
    if(isSelected()) painter->setBrush(QColor(255,255,0));
    painter->drawEllipse(BOUNDINGRECT);
    painter->drawText(0,0,pCity->name());

    if(next!=nullptr){
        painter->setPen(QPen(LINECOLOR,3));
        painter->drawLine(center(),QPointF(mapFromItem(next,next->center())));
    }
}

void GraphicsCity::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
{
    hover = true;
    QGraphicsItem::hoverEnterEvent(event);
}

void GraphicsCity::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
{
    hover = false;
    QGraphicsItem::hoverEnterEvent(event);
}

void GraphicsCity::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
    emit pressed(pCity);
    QGraphicsItem::mousePressEvent(event);
}
